package com.bank.backend.boot.server.service.impl;


import com.bank.backend.boot.base.constant.BankUserConstants;
import com.bank.backend.boot.base.model.dto.BankUserDTO;
import com.bank.backend.boot.base.model.entity.BankUser;
import com.bank.backend.boot.base.model.exception.GlobalException;
import com.bank.backend.boot.base.model.vo.TokenVO;
import com.bank.backend.boot.base.utils.JwtUtil;
import com.bank.backend.boot.base.utils.Md5Util;
import com.bank.backend.boot.server.mapper.BankUserMapper;
import com.bank.backend.boot.server.service.LoginService;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @Author JiaoWei
 * @Date 2024/3/9 16:12
 * @description: 用户服务实现
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Resource
    private BankUserMapper bankUserMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void register(BankUserDTO bankUserDto) {
        if (null == bankUserDto.getPhone()) {
            throw new GlobalException("手机号为空");
        }
        BankUser bankUser = bankUserMapper.getBankUserByPhone(bankUserDto.getPhone());
        if (bankUser != null) {
            throw new GlobalException("该用户已存在");
        }

        BankUser newBankUser = new BankUser();
        newBankUser.setPhone(bankUserDto.getPhone());
        newBankUser.setPassword(Md5Util.getMD5String(bankUserDto.getPassword()));
        newBankUser.setUsername(bankUserDto.getUsername());
        newBankUser.setAvatar(BankUserConstants.avatar);
        newBankUser.setLevel(BankUserConstants.level);
        newBankUser.setBankNumber(BankUserConstants.bankNumber);
        newBankUser.setBankBranchId(bankUserDto.getBankBranchId());
        newBankUser.setCreateTime(new Date());
        newBankUser.setUpdateTime(new Date());


        int i = bankUserMapper.registerBankUser(newBankUser);
        if (i != 1) {
            throw new GlobalException("注册失败");
        }
    }

    @Override
    public TokenVO login(BankUserDTO userDto) {
        BankUser bankUser = bankUserMapper.getBankUserByPhone(userDto.getPhone());
        if (bankUser == null) {
            throw new GlobalException("账号不存在");
        }
        if (userDto.getPassword() == null || "".equals(userDto.getPassword())) {
            throw new GlobalException("密码为空");
        }
        if (Md5Util.getMD5String(userDto.getPassword()).equals(bankUser.getPassword())) {
            // 生成token
            String token = JwtUtil.createJWT(String.valueOf(bankUser.getId()));
            // 将token存入redis
            ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue();
            opsForValue.set(token, token, 3, TimeUnit.HOURS);
            String refreshToken = JwtUtil.refreshToken(token);
            Date expireTime = new Date(System.currentTimeMillis() + JwtUtil.JWT_TTL);
            // 返回token
            return new TokenVO(token, refreshToken, expireTime);
        }else {
            throw new GlobalException("密码错误");
        }
    }
}
